package com.example.jetpackcomposebasic.todo.viewmodel.impl

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.example.jetpackcomposebasic.todo.ToDoItem

/**
 * 基于LiveData实现的ViewModel
 */
class LiveDataViewModel : ViewModel() {

    private val _toDoList: MutableLiveData<List<ToDoItem>> = MutableLiveData(listOf())

    val toDoList: LiveData<List<ToDoItem>> = _toDoList

    fun add(todo: ToDoItem) {

        _toDoList.value = _toDoList.value!! + listOf(todo)
    }

    fun remove(todo: ToDoItem) {
        _toDoList.value = _toDoList.value?.toMutableList()?.also {
            it.remove(todo)
        }


    }
}